<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_leave extends CI_Controller {

 
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('hrm/user/leave_model');	
		$this->load->model('hrm/user/attendance_model');		
	}
		
	function index()
	{
		//$data['leaves'] = $this->leave_model->get_all_leaves();
		$data['total_leave'] = $this->leave_model->total_leaves();
		$data['pending_leaves'] = $this->leave_model->get_pending_leaves();
		$data['leave_status'] = $this->leave_model->get_leave_status();
		$data['leave_granted'] = $this->leave_model->total_leave_granted();
		$data['leave_remaning'] = 12 - $data['leave_granted'];
		$meta['page_title'] = 'Request Leave Page';
	 	$meta['dashboard'] = 'dashboard';
		$this->load->view('common/header',$meta);
	 	$this->load->view('hrm/user/request-leave',$data);
     	$this->load->view('common/footer');		
	}
	
   function apply_leave()
   {
 	 $meta['page_title'] = "Welcome to leave mangement ";
	 $edit_id = $this->input->post('edit_id');
	 	if(!empty($edit_id))
		{			
			$this->form_validation->set_rules('start_date', 'Date', 'required');
			$this->form_validation->set_rules('reason', 'Reason', 'required');
			$this->form_validation->set_rules('edit_id', 'Id', 'required');
			//echo "sadsad";
			//exit;
		}
		
		else {
		$this->form_validation->set_rules('start_date', 'Date', 'required|callback_check_date');
		$this->form_validation->set_rules('reason', 'Reason', 'required');
	
		
		}
	

if ($this->form_validation->run() == FALSE)
{
	/*$this->form_validation->set_message('start_date');
	$this->form_validation->set_message('reason');*/
		//$data['leaves'] = $this->leave_model->get_all_leaves();
		//echo "asdasdasdsad";
		$data['total_leave'] = $this->leave_model->total_leaves();
		$data['pending_leaves'] = $this->leave_model->get_pending_leaves();
		$data['leave_status'] = $this->leave_model->get_leave_status();
		$data['leave_granted'] = $this->leave_model->total_leave_granted();
		$data['leave_remaning'] = 12 - $data['leave_granted'];
		$meta['page_title'] = 'Request Leave Page';
		$meta['dashboard'] = 'dashboard';
	 	$this->load->view('common/header',$meta);
	 	$this->load->view('hrm/request-leave',$data);
     	$this->load->view('common/footer');	
		//redirect('user/leave');
	
}
	else {
		
		if(!empty($edit_id))
		{
		$update_leave = array(
		'user_id' => $this->session->userdata('user_id'),
		'leave_date' => $this->input->post('start_date'),
		'reason' => $this->input->post('reason'),
		);
			$this->leave_model->update($update_leave,$edit_id);
			$this->session->set_flashdata('leave_message', 'Leave Updated Successfully');
			redirect('hrm/user_leave','refresh');
			
		}
		
		else {		
			
		$leave = array(
		'user_id' => $this->session->userdata('user_id'),
		'leave_date' => $this->input->post('start_date'),
		'reason' => $this->input->post('reason'),
		'date_applied' => $this->attendance_model->get_server_date(),
		'status' => 'pending'
		);	
		//echo "aasdasdasd";
		//print_r($leave);
		//exit;	
		$this->leave_model->insert_user_leave($leave);	
		
		$this->session->set_flashdata('leave_message', 'Leave Succesfully Applied');
		redirect('hrm/user_leave','refresh');		
		}
	}
		
   }
	
	function check_date($str)
	{	
	 $server_date = $this->attendance_model->get_server_date();
	
	if($str == $server_date)
	{
		$this->form_validation->set_message('check_date', 'You cannot apply leave for the same date');
		return FALSE;
	}
	elseif($this->leave_model->duplicate_date($str)>0)
	{
		$this->form_validation->set_message('check_date', 'You have already applied leave on this date.');
		return FALSE;
	}
	else {
		return TRUE;
	}
}

function delete($id)
{
$this->leave_model->delete($id);
$this->session->set_flashdata('leave_message', 'Leave Deleted Successfully');
redirect('hrm/user_leave','refresh');
}

function edit($edit_id)
	{
		$data = $this->leave_model->edit($edit_id);
		//print_r($data);
		$data['edit'] = array(
		'id' => $data['id'],
		'start_date'=> $data['leave_date'],
		'reason' => $data['reason'],
		);
		
		$data['total_leave'] = $this->leave_model->total_leaves();
		$data['pending_leaves'] = $this->leave_model->get_pending_leaves();
		$data['leave_status'] = $this->leave_model->get_leave_status();
		$data['leave_granted'] = $this->leave_model->total_leave_granted();
		$data['leave_remaning'] = 12 - $data['leave_granted'];
		$meta['page_title'] = 'Request Leave Page';
	 	$meta['dashboard'] = 'dashboard';
		$this->load->view('common/header',$meta);
	 	$this->load->view('hrm/request-leave',$data);
     	$this->load->view('common/footer');		
//$this->session->set_userdata($edit);
//redirect('user/leave');
		//print_r($edit);
	}
}
	
	?>